/*
 * $Id: TranslationFailedException.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.support.translator;

import org.springframework.util.ObjectUtils;

/**
 * Exception to be thrown when an actual type translation attempt fails.
 */
@SuppressWarnings("serial")
public final class TranslationFailedException extends TranslationException {
    /**
     * The source type.
     */
    private final Class<?> sourceType;
    /**
     * The target type.
     */
    private final Class<?> targetType;

    /**
     * The value.
     */
    private final Object value;

    /**
     * Create a new translation exception.
     * @param sourceType the value's original type
     * @param targetType the value's target type
     * @param value the value we tried to translate
     * @param cause the cause of the translation failure
     */
    public TranslationFailedException(Class<?> sourceType, Class<?> targetType, Object value, Throwable cause) {
        super("Failed to translate from type " + sourceType + " to type " + targetType + " for value '" + ObjectUtils.nullSafeToString(value) + "'", cause);
        this.sourceType = sourceType;
        this.targetType = targetType;
        this.value = value;
    }

    /**
     * Return the source type we tried to translate the value from.
     */
    public Class<?> getSourceType() {
        return this.sourceType;
    }

    /**
     * Return the target type we tried to translate the value to.
     */
    public Class<?> getTargetType() {
        return this.targetType;
    }

    /**
     * Return the offending value.
     */
    public Object getValue() {
        return this.value;
    }
}
